<?php
class Paginator{
	
	const IMAGE_NEXT = 'next.png';
	const IMAGE_PREV = 'previous.png';
	
	static function paginationUrl($n, $params=null){
		if(is_null($params)) $params=$_GET;
		$str = '?';
		if(!isset($params['begin']) || is_array($params['begin'])){
			$params['begin'] = 0;
		}
		$params['begin'] = max(intval($params['begin'])+$n, 0);
		foreach($params as $param => $value){
			$str.=$param .'='. $value .'&';
		}
		return substr($str, 0, -1); //remove last '&'
	}
	
	static function paginatorHtml($max, $step, $params=null){
		if(is_null($params)) $params=$_GET;
		$paginator = '';
		$paramsStr = '?';
		$pageBegin = 0;
		if(isset($params['begin'])){
			$pageBegin = intval($params['begin']);
			unset($params['begin']);
		}
		foreach($params as $param => $value){
			$paramsStr.=$param .'='. $value .'&';
		}
		if($step>0){
			for($i=0; $i<=$max; $i+=$step) {
				$current = ($pageBegin>=$i && $pageBegin<$i+$step);
				$paginator.='<a '.($current?'class="current" ':'').'href="'.$paramsStr.'begin='.$i.'">['.$i.'-'.min($i+$step, $max).']</a> ';
			}
		}
		return $paginator;
	}
}